import { createRouter, createWebHistory} from 'vue-router';
import DataManage from '../views/DataManage.vue';
import store from '../store'; // 引入store

const routes = [
  {
    path: '/login',
    name: 'UserLogin',
    component: () => import('../views/denglu.vue') // 确保路径正确
  },
  {
    path: '/page',
    name: 'page',
    component: () => import('../views/homePage.vue')
  },
  {
    path: '/gl',
    name: 'DataManage',
    component: DataManage
  },
  {
    path:"/",
    redirect:"/page"
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})
router.beforeEach((to, from, next) => {
  const publicPages = ['/login'];
  const requiresAuth = !publicPages.includes(to.path);

  const isAuthenticated = store.state.isAuthenticated; // 使用VueX中的认证状态

  if (requiresAuth && !isAuthenticated) {
    next('/login');
  } else {
    next();
  }
});

export default router
